Ext.define('vacake.controller.Register', {
    extend: 'Ext.app.Controller',

    config: {
        routes: {
            'loadRegister': 'loadRegister'
        },
        refs: {
            mainView: 'mainView',
            registerView: 'registerForm',
            regBackParentButton: 'button[action=reg_back_parent]',
            registerSubmitButton: 'button[action=registerSubmit]',
//            regShowPasswordButton: 'button[action=reg_show_password]',
            regShowPassword: 'registerForm #reg_show_password',
            regFieldSet: '#reg_field',
            textMobileNum: '#mobile_num',
            textPassword: '#password',
            textRePassword: '#repassword',
            textHiddenCaid: '#hidden_caid'
        },
        control: {
            regBackParentButton: {
                tap: 'regBackParentButton_onTap'
            },
            registerSubmitButton: {
                tap: 'registerSubmitButton_onTap'
            },
            regShowPassword: {
                check: 'regShowPasswordView',
                uncheck: 'regUnShowPasswordView'
            }
        }
    },

    /*路由中初始化数据*/
    loadRegister: function () {
        var registerView = this.getRegisterView();
        if (registerView == null) {
            registerView = Ext.create("vacake.view.Register");
        }
        Ext.Viewport.animateActiveItem(registerView, {type: 'slide', direction: 'left'});
    },
    /*注册页面上方的返回按钮*/
    regBackParentButton_onTap: function () {
         var backid = Ext.Viewport.getInnerItems().length - 2;
         Ext.Viewport.remove(this.getRegisterView(), true);
         Ext.Viewport.setActiveItem(backid);
    },
    /*点击注册按钮*/
    registerSubmitButton_onTap: function () {
        Ext.Viewport.mask({
            xtype: 'loadmask',
            message: '注册中...'
        });
        /*判定是否存在显示密码框*/
        if (this.getRegFieldSet().child('textfield[name=viewPassword]') != undefined) {
            this.getTextPassword().setValue(this.getRegFieldSet().child('textfield[name=viewPassword]').getValue());
            this.getTextRePassword().setValue(this.getRegFieldSet().child('textfield[name=viewRePassword]').getValue());
        }
        var mobileNum = this.getTextMobileNum().getValue();
        var password = this.getTextPassword().getValue();
        var repassword = this.getTextRePassword().getValue();
        if (mobileNum == '' || password == '' || repassword == '') {
            Ext.Viewport.unmask();
//            Ext.Msg.alert('手机，密码或确认密码为空！');
            cordova.exec(function (message) {
            }, function (error) {
            }, "Messages", "showMsg", ['手机，密码或确认密码为空！']);
            return false;
        }
        if (!((/^13\d{9}$/g.test(mobileNum)) || (/^15[0-35-9]\d{8}$/g.test(mobileNum)) || (/^18[0-35-9]\d{8}$/g.test(mobileNum)))) {
            Ext.Viewport.unmask();
            cordova.exec(function (message) {
            }, function (error) {
            }, "Messages", "showMsg", ['请填写正确的手机号码！']);
//            Ext.Msg.alert('请填写正确的手机号码！');
            return;
        }
        if (password != repassword) {
            Ext.Viewport.unmask();
            cordova.exec(function (message) {
            }, function (error) {
            }, "Messages", "showMsg", ['密码和确认密码不一致！']);
            // Ext.Msg.alert('密码和确认密码不一致！');
            return;
        }
        this.getTextHiddenCaid().setValue(Global.app_id);
        this.getRegisterView().submit({
            url: Global.api_url + '/cloud/1/users_register',
            method: 'POST',
            scope: this,
            success: function (from, result) {
                Ext.Viewport.unmask();
                cordova.exec(function (message) {
                }, function (error) {
                }, "Messages", "showMsg", [result.Variables.Message.messagestr]);
//                Ext.Msg.alert(result.Variables.Message.messagestr);
                if (result.Variables.Result.code == 0) {
                    window.localStorage.setItem('app_Login_id', result.Variables.member_uid);//设置
                    window.localStorage.setItem('app_Login_name', result.Variables.member_username);
                    window.localStorage.setItem('app_Login_avatar', result.Variables.avatar);
                    //将登陆用户提交给cordova中
                    cordova.exec(function(winParam) {},function(error) {}, "GetApplicationInfo", "getApplicationInfo", [result.Variables.member_uid]);
                    Ext.Viewport.remove(this.getRegisterView(), true);
                }
            },
            failure: function () {//请求失败时执行操作
                Ext.Viewport.unmask();
                cordova.exec(function (message) {
                }, function (error) {
                }, "Messages", "showMsg", ['请求失败,服务器维护中...']);
//                Ext.Msg.alert('请求失败', '服务器维护中....');
            }
        });
    },
    /*显示和隐藏密码*/
    regShowPasswordView: function (checkBox, e) {
        checkBox.setLabel('隐藏密码');
        var viewPassword = {xtype: 'textfield', name: 'viewPassword', value: this.getTextPassword().getValue(), placeHolder: '密    码'};
        var viewRePassword = {xtype: 'textfield', name: 'viewRePassword', value: this.getTextRePassword().getValue(), placeHolder: '确认密码'};
        this.getTextPassword().hide();
        this.getTextRePassword().hide();
        this.getRegFieldSet().add(viewPassword);
        this.getRegFieldSet().add(viewRePassword);
    },
    regUnShowPasswordView: function (checkBox, e) {
        checkBox.setLabel('显示密码');
        this.getTextPassword().setValue(this.getRegFieldSet().child('textfield[name=viewPassword]').getValue());
        this.getTextRePassword().setValue(this.getRegFieldSet().child('textfield[name=viewRePassword]').getValue());
        this.getTextPassword().show();
        this.getTextRePassword().show();
        this.getRegFieldSet().remove(this.getRegFieldSet().child('textfield[name=viewPassword]'));
        this.getRegFieldSet().remove(this.getRegFieldSet().child('textfield[name=viewRePassword]'));
    }
});